//app.js
var storage = require("utils/storage.js");
var api = require("./api.js");
var infoUtils = require("utils/userInfo.js");
import getData from './utils/agriknow.js'
App({
  onLaunch: function () {
    //隐藏系统tabbar
    wx.hideTabBar();
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    var _this = this;
    // _this.getWxUserInfo();
    _this.getManager();
  },
  /** 全局变量的设置 */
  globalData: {
    /** 全局的open_id */
    open_id: null,
    defaultCity: null,
    defaultCounty: null,
    wxUserInfo: {},
    userInfo: {},
    page: null,
    time: null,
    type: '',
    userId: '',
    systeminfo: {},
    tabBar: {
      color: "#6e6d6b",
      selectedColor: "#ff4081",
      borderStyle: "white",
      backgroundColor: "#fff",
      list: [{
          pagePath: "/pages/index/index",
          iconPath: "icon/tab1.png",
          selectedIconPath: "icon/tab1_selected.png",
          text: "首页"
        },
        {
          pagePath: "/pages/audio/audio",
          iconPath: "icon/tab2.png",
          selectedIconPath: "icon/tab2_selected.png",
          text: "智库"
        },

        {
          pagePath: "/pages/middle/middle",
          iconPath: "icon/tab3.png",
          isSpecial: true,
          text: "足迹"
        },
        {
          pagePath: "/pages/discover/discover",
          iconPath: "icon/tab4.png",
          selectedIconPath: "icon/tab4_selected.png",
          text: "班级",
        },
        {
          pagePath: "/pages/mine/mine",
          iconPath: "icon/tab5.png",
          selectedIconPath: "icon/tab5_selected.png",
          text: "我的"
        }
      ]
    }
  },

  getManager: function () {
    console.log(wx.canIUse("getUpdateManager") + '123213')
    // 用户版本更新
    // if (wx.canIUse("getUpdateManager")) {
    //     let updateManager = wx.getUpdateManager();
    //     updateManager.onCheckForUpdate((res) => {
    //         // 请求完新版本信息的回调
    //         console.log(res.hasUpdate);
    //     })
    //     updateManager.onUpdateReady(() => {
    //         wx.showModal({
    //             title: '更新提示',
    //             content: '新版本已经准备好，是否重启应用？',
    //             success: (res) => {
    //                 if (res.confirm) {
    //                     // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
    //                     updateManager.applyUpdate();
    //                 } else if (res.cancel) {
    //                     return false;
    //                 }
    //             }
    //         })
    //     })
    //     updateManager.onUpdateFailed(() => {
    //         // 新的版本下载失败
    //         wx.hideLoading();
    //         wx.showModal({
    //             title: '升级失败',
    //             content: '新版本下载失败，请检查网络！',
    //             showCancel: false
    //         });
    //     });
    // }

  },
  getData: new getData(),
  onShow: function () {
    // console.log(12312)
    //隐藏系统tabbar
    wx.hideTabBar({
      fail: function () {
        setTimeout(function () {
          wx.hideTabBar();
        }, 500)
      }
    });
  },
  onReady: function () {
    wx.hideTabBar({
      fail: function () {
        setTimeout(function () {
          wx.hideTabBar()
        }, 500)
      }
    })
  },
  // 查看是否授权
  is_empower: function () {
    var _this = this;
    /** 获取open_id */
    var open_id = _this.globalData.open_id;
    /** 如果open_id 为 undefined 则获取用户信息 */
    if (open_id == undefined || open_id == null) {
      new Promise((resolve, reject) => {
        wx.getSetting({
          success(res) {
            if (res.authSetting['scope.userInfo']) {
              // 已经授权，可以直接调用 getWxUserInfo 获取头像昵称
              _this.getWxUserInfo();
            } else {
              wx.navigateTo({
                url: '/pages/welcome/welcome',
              })
            }
            resolve(res)
          }
        })
      })
    }
  },
  get_open_id: function () {
    var _this = this;
    /** 获取open_id */
    var open_id = _this.globalData.open_id;
    /** 如果open_id 为 undefined 则获取用户信息 */
    if (open_id == undefined || open_id == null) {
      new Promise((resolve, reject) => {
        wx.getSetting({
          success(res) {
            if (res.authSetting['scope.userInfo']) {
              // 已经授权，可以直接调用 getWxUserInfo 获取头像昵称
              _this.getWxUserInfo();
            }
            resolve(res)
          }
        })
      })
    }
  },
  getWxUserInfo: function (e) {
    var _this = this;
    wx.getSystemInfo({
      success: function (res) {
        _this.globalData.systeminfo = res;
      },
    })
    return new Promise(function (resolve, reject) {
      if (_this.globalData.userInfo == {} || JSON.stringify(_this.globalData.userInfo) == '{}') {
        //调用登录接口
        wx.login({
          success: function (redd) {
            if (redd.code) {
              wx.request({
                url: api.login.getOpenId + '/' + redd.code,
                success: function (back) {
                  if (back.statusCode == 200) {
                    var open_id = back.data.data;
                    wx.getUserInfo({
                      lang: 'zh_CN',
                      success: function (result) {
                        let wxUserInfo = result.userInfo;
                        _this.globalData.wxUserInfo.openId = open_id;
                        /** 全局变量赋值  open_id */
                        _this.globalData.open_id = open_id;
                        wx.request({
                          url: api.login.login,
                          data: {
                            openId: open_id,
                            nickName: wxUserInfo.nickName,
                            avatarUrl: wxUserInfo.avatarUrl,
                            gender: wxUserInfo.gender,
                            province: wxUserInfo.province,
                            city: wxUserInfo.city,
                          },
                          header: {
                            'content-type': 'application/x-www-form-urlencoded' // 默认值
                          },
                          method: 'post',
                          success: function (backResult) {
                            if (backResult.statusCode == 200 && backResult.data.code == 200) {
                              _this.globalData.userInfo = backResult.data.data;
                            }
                            resolve(200);
                          }
                        })
                      },
                      fail: function (res) {
                        console.log('获取用户信息失败')
                      }
                    })
                  } else {
                    console.log("getOpenIdERR:", back);
                  }
                }
              })
            } else {
              console.log('获取用户信息失败');
              // reject(600);
              //获取用户信息失败后。请跳转授权页面
              wx.showModal({
                title: '警告',
                content: '尚未进行授权，请点击确定跳转到授权页面进行授权。',
                success: function (res) {
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '/pages/welcome/welcome',
                    })
                  }
                }
              })
            }
          }
        })
      } else {
        resolve(200);
      }
    });

  },
  editTabbar: function () {
    let tabbar = this.globalData.tabBar;
    let currentPages = getCurrentPages();
    let _this = currentPages[currentPages.length - 1];
    let pagePath = _this.route;
    pagePath.indexOf("/") != 0 && (pagePath = "/" + pagePath);
    for (let i in tabbar.list) {
      tabbar.list[i].selected = false;
      tabbar.list[i].pagePath == pagePath && (tabbar.list[i].selected = true);
    }
    _this.setData({
      tabbar: tabbar
    });
  },

  /** -----------------------------------  动画效果   ---------------------------------- */

  //渐入，渐出实现 
  show: function (that, param, opacity) {
    var animation = wx.createAnimation({
      //持续时间800ms
      duration: 800,
      timingFunction: 'ease',
    });
    //var animation = this.animation
    animation.opacity(opacity).step()
    //将param转换为key
    var json = '{"' + param + '":""}'
    json = JSON.parse(json);
    json[param] = animation.export()
    //设置动画
    that.setData(json)
  },

  //滑动渐入渐出
  slideupshow: function (that, param, px, opacity) {
    var animation = wx.createAnimation({
      duration: 800,
      timingFunction: 'ease',
    });
    animation.translateY(px).opacity(opacity).step()
    //将param转换为key
    var json = '{"' + param + '":""}'
    json = JSON.parse(json);
    json[param] = animation.export()
    //设置动画
    that.setData(json)
  },

  //向右滑动渐入渐出
  sliderightshow: function (that, param, px, opacity) {
    var animation = wx.createAnimation({
      duration: 800,
      timingFunction: 'ease',
    });
    animation.translateX(px).opacity(opacity).step()
    //将param转换为key
    var json = '{"' + param + '":""}'
    json = JSON.parse(json);
    json[param] = animation.export()
    //设置动画
    that.setData(json)
  },
  /** 调查问卷单选题目 */
  radio_report_data: [{
      title: '3）、您的企业品牌现状？',
      question_data: [{
          id: 1,
          name: '创新盈利模式',
        },
        {
          id: 2,
          name: '资本营作与投融策略'
        },
        {
          id: 3,
          name: '互联网营销'
        },
        {
          id: 4,
          name: '销售管理',
        }
      ],
      current_name: 'q_3_current'
    },
    {
      title: '4）、您的企业品牌观念？',
      question_data: [{
          id: 1,
          name: '具有品牌战略实施的紧迫感',
        },
        {
          id: 2,
          name: '品牌战略概念模糊'
        },
        {
          id: 3,
          name: '不知从何着手'
        }
      ],
      current_name: 'q_4_current'
    },
    {
      title: '5）、您的品牌战略期许？',
      question_data: [{
          id: 1,
          name: '打造品牌知名度',
        },
        {
          id: 2,
          name: '扩充市场占有率'
        },
        {
          id: 3,
          name: '建立新品牌'
        },
        {
          id: 4,
          name: '其它'
        }
      ],
      current_name: 'q_5_current'
    },
    {
      title: '6）、您要做的品牌类别？',
      question_data: [{
          id: 1,
          name: '食用饮料业品牌',
        },
        {
          id: 2,
          name: '日用化工业品牌'
        },
        {
          id: 3,
          name: '汽车机械业品牌'
        },
        {
          id: 4,
          name: '商业品牌'
        },
        {
          id: 5,
          name: '服务业品牌',
        },
        {
          id: 6,
          name: '女装品牌'
        },
        {
          id: 7,
          name: '其他服装品牌'
        },
        {
          id: 8,
          name: '网络信息业品牌'
        }
      ],
      current_name: 'q_6_current'
    },
    {
      title: '7）、您希望的品牌个性？',
      question_data: [{
          id: 1,
          name: '真诚',
        },
        {
          id: 2,
          name: '刺激'
        },
        {
          id: 3,
          name: '强壮'
        },
        {
          id: 4,
          name: '教养'
        },
        {
          id: 4,
          name: '能力'
        }
      ],
      current_name: 'q_7_current'
    },
    {
      title: '8）、以下品牌传播方式，您更青睐的是？',
      question_data: [{
          id: 1,
          name: '用户传播',
        },
        {
          id: 2,
          name: '广告策划'
        },
        {
          id: 3,
          name: '公关策划'
        },
        {
          id: 4,
          name: '促销策划'
        },
        {
          id: 4,
          name: '媒介传播'
        },
        {
          id: 4,
          name: '整合传播'
        }
      ],
      current_name: 'q_8_current'
    },
    {
      title: '9）、您需要的品牌服务？',
      question_data: [{
          id: 1,
          name: '品牌定位',
        },
        {
          id: 2,
          name: '品牌形象建立'
        },
        {
          id: 3,
          name: '品牌形象整合传播'
        },
        {
          id: 4,
          name: '品牌维护'
        }
      ],
      current_name: 'q_9_current'
    },
    {
      title: '10）、您的品牌服务预算？',
      question_data: [{
          id: 1,
          name: '10万以内',
        },
        {
          id: 2,
          name: '50万以内'
        },
        {
          id: 3,
          name: '100万以内'
        },
        {
          id: 4,
          name: '500万以内'
        }
      ],
      current_name: 'q_10_current'
    }


  ]



});